<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Services\H5Service;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cookie;

class H5Controller extends Controller
{
    /**
     * 保存答案.
     */
    public function saveAnswer(Request $request)
    {
        $this->validate(
            $request,
            [
                'phone'     => 'regex:/^1[3456789][0-9]{9}$/', // 手机号
            ],
            [
                'phone.*'  => '手机号码格式有误！',
            ]
        );
        $clientInfo = [
            'ip'           => $request->getClientIp(),
            'user_agent'   => $request->userAgent(),
        ];
        $res = H5Service::saveAnswer($clientInfo, $request->all());

        return successJson($res)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * 用户权限.
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function getUserPermission(Request $request)
    {
        $res = H5Service::getUserPermission($request->get('openid', ''), $request->all());

        return successJson($res)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * 用户权限重置.
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function resetPermission(Request $request)
    {
        $res = H5Service::resetPermission($request->get('openid', ''), $request->all());

        return successJson($res)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * 用户权限重置.
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function doCookie(Request $request)
    {
        $type = $request->get('type', 0);
        $res  = [];
        if (1 == $type) {
            $res = ['openid' => cookie('user_openid')];
        } else {
            Cookie::queue(
                Cookie::make('user_openid', '', 1, '/', 'ithook.cn', false, false)
            );
        }

        return successJson($res)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * 获取没有购买的列表.
     *
     * @return JsonResponse
     */
    public function getIsBuyNullList()
    {
        return successJson(H5Service::getIsBuyNullList())
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * 保存是否已经购买结果.
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function saveIsBuy(Request $request)
    {
        return successJson(H5Service::saveIsBuy($request->get('id', 0)))
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }
}
